package fm.last.time.stats;



import java.io.InputStream;
import java.net.URI;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.R;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ArrayAdapter;

public abstract class LastFmRequester extends AsyncTask<String, Integer, String>{
    String urlPrefix = "http://ws.audioscrobbler.com/2.0/?method=";
    protected static final String FORMAT = "format";
    protected static final String JSON = "json";
    public static final String API_KEY_KEY = "api_key";

	protected static final String USERNAME_KEY = "username";
	protected static final String METHOD_KEY = "method";
    public LastFmRequester(String method){
    	this.urlPrefix = urlPrefix+method+'&';
    }


    protected InputStream getData(Map<String, String> parameters) throws Exception{
        HttpClient client = new DefaultHttpClient();
        String uri = makeUrlString(parameters);
        Log.d("somethign", uri);
        HttpGet request = new HttpGet(new URI(makeUrlString(parameters)));

        HttpResponse response = client.execute(request);
        return response.getEntity().getContent();
    }

    private String makeUrlString(Map<String, String> parameters) {
		StringBuilder urlBuffer = new StringBuilder(urlPrefix);
    	urlBuffer.append(makeParameterString(parameters));
		return urlBuffer.toString();
	}


	protected String makeParameterString(Map<String, String> parameters) {
		StringBuilder paramBuilder = new StringBuilder();
		ArrayList<String> keyList = new ArrayList<String>(parameters.keySet());
		Collections.sort(keyList);
		for(Iterator<String> i = keyList.iterator(); i.hasNext();){
			String key = i.next();
			paramBuilder.append(key);
			paramBuilder.append('=');
			paramBuilder.append(parameters.get(key));
			if(i.hasNext())
				paramBuilder.append('&');
		}
		return paramBuilder.toString();
	}

	protected String makeSignatureString(Map<String, String> parameters) {
		StringBuilder paramBuilder = new StringBuilder();
		ArrayList<String> keyList = new ArrayList<String>(parameters.keySet());
		Collections.sort(keyList);
		for(Iterator<String> i = keyList.iterator(); i.hasNext();){
			String key = i.next();
			paramBuilder.append(key);
			paramBuilder.append(parameters.get(key));
		}
		return paramBuilder.toString();
	}

	@Override
    protected void onPostExecute(String stocks){
		Log.e("onPostEx", stocks);
    }
	
	public String md5(String s) {  
	    try {  
	        // Create MD5 Hash  
	        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");  
	        digest.update(s.getBytes());  
	        byte messageDigest[] = digest.digest();  
	        StringBuilder builder = new StringBuilder();
            for(byte b : messageDigest) {
                builder.append(Integer.toHexString((b >> 4) & 0xf));
                builder.append(Integer.toHexString(b & 0xf));
            }
	        return builder.toString();  
	    } catch (NoSuchAlgorithmException e) {  
	        e.printStackTrace();  
	    }  
	    return "";  
	} 

}


